﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Smart.Web.Mobile;

namespace MobileWebMvc.Helpers
{
    /// <summary>
    /// 
    /// </summary>
    public static class MobileHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static string Xml(this HtmlHelper helper)
        {
            return String.Format( "<?xml version=\"1.0\" encoding=\"{0}\" ?>",
                                  helper.ViewContext.HttpContext.Response.ContentEncoding.WebName );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static string Charset(this HtmlHelper helper)
        {
            return String.Format( "<meta http-equiv=\"Content-Type\" content=\"text/html; {0}\" />",
                                  helper.ViewContext.HttpContext.Response.ContentEncoding.WebName );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="carrier"></param>
        /// <param name="no"></param>
        /// <returns></returns>
        public static string Pictgram(this HtmlHelper helper, Carrier carrier, int no)
        {
            Pictgram pictgram = MobileSettings.Default.CarrierSettings[carrier].GetPictgramByNo( no );
            if( pictgram == null )
            {
                return "";
            }

            return new string( new char[] { pictgram.Unicode } );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string NoPictgramPC(this HtmlHelper helper, string src)
        {
            if( WebMobileContext.Current.IsMobile )
            {
                return src;
            }

            return MobileUtil.ErasePictgram( src );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Hex(this HtmlHelper helper, string value)
        {
            if( String.IsNullOrEmpty( value ) )
            {
                return value;
            }

            StringBuilder sb = new StringBuilder();
            foreach( char c in value )
            {
                sb.Append( MobileUtil.UnicodeToHex( c ) );
                sb.Append( " " );
            }

            sb.Length = sb.Length - 1;

            return sb.ToString();
        }
    }
}